﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fcom.VNInsurance.Web.Models;

namespace Fcom.VNInsurance.Web.Controllers
{
     [Authorize(Roles = "13,14")]
    public class CoverageController : Controller
    {
        //
        // GET: /Coverage/

        Fcom.VNInsurance.Web.Models.VNInsuranceDataContext db = new Models.VNInsuranceDataContext();
        public ActionResult Index()
        {
            return View();
        }

       

        [HttpGet]
        public ActionResult AddCoverage()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AddCoverage(Coverage co)
        {
            Coverage cpn = new Coverage();
            var isexit = db.Coverages.Where(m => m.Name == co.Name).FirstOrDefault();
            if (isexit == null)
            {
                cpn.Name = co.Name;
                db.Coverages.InsertOnSubmit(cpn);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            else
            {
                return RedirectToAction("Index");
            }
        }
        public ActionResult CoverageExtent(int ID)
        {
            var coverage = db.Coverages.Where(m => m.ID == ID);

            return View(coverage.FirstOrDefault());
  
        }
        public ActionResult jsonCoverageExtentBenefit(int ID)
        {
            var coverageextent = from b in db.CoverageExtentBenefits
                                 where b.CoverageID == ID
                                 select new {b.CoverageID,b.BenefitID,b.isAdded,BenefitName= b.Benefit.BenefitLangs.Where(m=>m.LangID==Request.Cookies["_culture"]["lang"]).FirstOrDefault().DisplayName,CoverageName=b.Benefit.Coverage.Name};
            return Json(coverageextent,JsonRequestBehavior.AllowGet);
        }
     
        public ActionResult jsonBenefitbyCoverage(int ID)
        {
            var coverageextent = from b in db.CoverageExtentBenefits
                                 where b.CoverageID == ID
                                 select new { b.BenefitID, DisplayName=b.Benefit.BenefitLangs.Where(n => n.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault().DisplayName };
            var benefits = (from m in db.BenefitLangs where m.Benefit.CoverageID == ID && m.LangID == Request.Cookies["_culture"]["lang"] select new { m.BenefitID,m.DisplayName}).Except(coverageextent);
           return Json(benefits, JsonRequestBehavior.AllowGet);
        }
        public ActionResult AddExtenBenefitCoverage(int ID)
        {
            var coverage = db.Coverages.Where(m => m.ID == ID).FirstOrDefault();
            ViewBag.Coverage = coverage;
            var coveragelistOther = db.Coverages.Where(m => m.ID != ID);
            ViewBag.CoverageOther = coveragelistOther;
            ViewBag.Benefits = db.BenefitLangs.Where(m => m.Benefit.CoverageID == coveragelistOther.FirstOrDefault().ID && m.LangID == Request.Cookies["_culture"]["lang"]);
            return View();
        }
         [HttpPost]
        public ActionResult AddExtenBenefitCoverage(Fcom.VNInsurance.Web.Models.CoverageExtentBenefit model)
        {
            if (ModelState.IsValid)
            {
                db.CoverageExtentBenefits.InsertOnSubmit(model);
                db.SubmitChanges();
                return RedirectToAction("CoverageExtent", new { ID=model.CoverageID});
            }
            return View();
        }
        public ActionResult jsonHopitalCoverage(int ID)
        {
            var coveragehopital = from cv in db.CoverageBaseHopitals 
                                  where cv.CoverageID == ID 
                                  select new { cv.CoverageID,cv.HopitalID,cv.Hopital.HopitalLangs.Where(m=>m.LangID==Request.Cookies["_culture"]["lang"]).FirstOrDefault().HopitalName};
            return Json(coveragehopital, JsonRequestBehavior.AllowGet);
           
        }
        public ActionResult AddHopitalCoverage(int ID)
        {
            var coverage = db.Coverages.Where(m => m.ID == ID);
            var hopitalused = from h in db.HopitalLangs where h.Hopital.CoverageBaseHopitals.Where(n=>n.CoverageID==ID).Count()>0&&h.LangID== Request.Cookies["_culture"]["lang"] select h;
    
            ViewBag.Coverage = coverage.FirstOrDefault();
            ViewBag.hopitalcat = db.HopitalLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"]).Except(hopitalused);
            return View();
        }
        public ActionResult removeHoptialCoverage(int HopitalID, int CoverageID)
        {
            var coveragehopital = db.CoverageBaseHopitals.Where(m => m.CoverageID == CoverageID && m.HopitalID == HopitalID).FirstOrDefault();
            db.CoverageBaseHopitals.DeleteOnSubmit(coveragehopital);
            db.SubmitChanges();
            return Json(new { success=true},JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public ActionResult AddHopitalCoverage(Fcom.VNInsurance.Web.Models.CoverageBaseHopital model)
        {
            if (ModelState.IsValid)
            {
                db.CoverageBaseHopitals.InsertOnSubmit(model);
                db.SubmitChanges();
                return RedirectToAction("HopitalCoverage", "Coverage", new { ID=model.CoverageID});
            }
            return View();

        }
        public ActionResult HopitalCoverage(int ID)
        {
            var coverage = db.Coverages.Where(m => m.ID == ID);
           
            return View(coverage.FirstOrDefault());
        }
        //
        public ActionResult jsonGaraCoverage(int ID)
        {
            var coveragegara = from cv in db.CoverageBaseGaras
                                  where cv.CoverageID == ID
                                  select new { cv.CoverageID, cv.GaraID, cv.Gara.GaraLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault().GaraName };
            return Json(coveragegara, JsonRequestBehavior.AllowGet);
        }
        public ActionResult AddGaraCoverage(int ID)
        {
            var coverage = db.Coverages.Where(m => m.ID == ID);
            var garaused = from h in db.GaraLangs where h.Gara.CoverageBaseGaras.Where(n => n.CoverageID == ID).Count() > 0 && h.LangID == Request.Cookies["_culture"]["lang"] select h;

            ViewBag.Coverage = coverage.FirstOrDefault();
            ViewBag.garacat = db.GaraLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"]).Except(garaused);
            return View();
        }
        public ActionResult removeGaraCoverage(int GaraID, int CoverageID)
        {
            var coveragegara = db.CoverageBaseGaras.Where(m => m.CoverageID == CoverageID && m.GaraID == GaraID).FirstOrDefault();
            db.CoverageBaseGaras.DeleteOnSubmit(coveragegara);
            db.SubmitChanges();
            return Json(new { success = true }, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public ActionResult AddGaraCoverage(Fcom.VNInsurance.Web.Models.CoverageBaseGara model)
        {
            if (ModelState.IsValid)
            {
                db.CoverageBaseGaras.InsertOnSubmit(model);
                db.SubmitChanges();
                return RedirectToAction("GaraCoverage", "Coverage", new { ID = model.CoverageID });
            }
            return View();

        }
        public ActionResult GaraCoverage(int ID)
        {
            var coverage = db.Coverages.Where(m => m.ID == ID);

            return View(coverage.FirstOrDefault());
        }

        //

        public JsonResult listCoverage()
        {
            var coverage = (from a in db.Coverages 
                               select new
                               {
                                   ID = a.ID,
                                   Name = a.Name,


                               });
            return Json(coverage, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        public ActionResult EditCoverage(int id)
        {
            var cover = db.Coverages.Where(m => m.ID == id).FirstOrDefault();
            try
            {
                return View(cover);

            }
            catch
            {
                return RedirectToAction("Index");
            }
        }
        [HttpPost]
        public ActionResult EditCoverage(Coverage autocoverage)
        {
            var foud = db.Coverages.Where(m => m.ID == autocoverage.ID).FirstOrDefault();
            if (foud != null)
            {
                foud.Name = autocoverage.Name;
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        [HttpPost]
        public ActionResult remove(int id)
        {
            var at = db.Coverages.Where(m => m.ID == id).FirstOrDefault();
           
             foreach(var a in at.Benefits)
             {
              
                 var lang = db.BenefitLangs.Where(m => m.BenefitID == a.ID);
                 db.BenefitLangs.DeleteAllOnSubmit(lang);
             }
           
            db.Benefits.DeleteAllOnSubmit(at.Benefits);
            db.Coverages.DeleteOnSubmit(at);
            db.SubmitChanges();
            return Json(true);
        }


    }
}
